package com.example.gybicycle.service;

import java.util.Date;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.sql.CrudService;
import com.example.gybicycle.common.util.DataUtil;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.dao.MemberInsureDao;
import com.example.gybicycle.entity.MemberInsure;
import com.example.gybicycle.entity.MemberOrder;
import com.example.gybicycle.dao.MemberOrderDao;
import com.example.gybicycle.entity.MemberPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MemberOrderService extends CrudService<MemberOrderDao, MemberOrder> {
    @Autowired
    private MemberInsureDao insureDao;
    @Override
    public MemberOrder get(String id) {
        MemberOrder table=super.get(id);
        return table;
    }
    public Result save(MemberOrder entity,String loginId){
        int num=0;
        if(entity.getId()==null){
            num=dao.insert(entity);
        }else {
            num=dao.update(entity);
        }
        if(num>0)
            return Result.success(entity);
        else 
            return Result.fail("保存失败");
    }

    @Override
    public int insert(MemberOrder entity) {
        entity.setId(NumberUtil.getNum(NumType.ORDER));
        entity.setStartTime(new Date());
        int num=super.insert(entity);
        return  num;
    }

    @Override
    public int insertByList(List<MemberOrder> list) {
        for(MemberOrder entity:list){
            entity.setId(NumberUtil.getNum(NumType.ORDER));
        }
        return super.insertByList(list);
    }

    public List<MemberOrder> findList(MemberOrder entity){
        List<MemberOrder> list=super.findList(entity);
        return list;
    }

    public MemberOrder closeOrder(String orderId){
        MemberOrder order=dao.get(orderId);
        order.setOrderState(2);
        order.setEndTime(new Date());
        order.setEndPoint("报修，无位置");
        order.setDuration(DataUtil.Duration(order.getStartTime(),order.getEndTime()));
        dao.update(order);
     /*   MemberInsure insure=new MemberInsure();
        insure.setMemberOrderId(orderId);
        insure.setEndTime(new Date());
        insureDao.updateByOrder(insure);*/
        return order;
    }

    /**
     * 普通骑行人
     * 结束订单
     * @param orderId
     * @param endTime
     * @return
     */
    public MemberOrder endOrder(String orderId,Date endTime){
        MemberOrder order1=dao.get(orderId);
        MemberOrder order=new MemberOrder();
        order.setId(orderId);
        order.setEndTime(endTime);
        order.setOrderState(2);
//        历时 xx分钟
        long duration= DataUtil.DurationMinutes(order1.getStartTime(),endTime);
        order.setDuration(String.valueOf(duration));
//        获取待支付金额
        MemberPay pay=new MemberPay();
        pay.setMemberOrderId(orderId);
        int  num=dao.update(order);
        if(num>0)
            return order;
        else return null;
    }

    public MemberOrder getOne(MemberOrder order){
        return dao.getOne(order);
    }

}